{% extends 'layout/manage.html' %}
{% load static %}
{% load issues %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugins/editor-md/css/editormd.min.css' %}">
    <link rel="stylesheet" href="{% static 'plugins/bootstrap-datepicker/css/bootstrap-datepicker.css' %}">
    <link rel="stylesheet" href="{% static 'plugins/bootstrap-select/dist/css/bootstrap-select.min.css' %}">
    <style>
        .issues-list .number {
            width: 100px;
            text-align: center;
        }

        .issues-list .number {
            font-weight: 500;
            padding: 0 10px;
        }

        .issues-list .issue .tags {
            padding: 10px 0;
        }

        .issues-list .issue .tags span {
            margin-right: 20px;
            display: inline-block;
            font-size: 12px;
        }

        .issues-list .issue .tags .type {
            color: white;
            padding: 1px 5px;
            border-radius: 5px;
        }

        .pd-0 {
            padding: 0 !important;
        }

        #addForm > div:nth-child(5) > div.col-sm-10 > div.error-msg {
            margin-top: -10px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid" style="padding: 20px 0;">
        <div class="col-sm-3">
            <div class="panel panel-default">
                <div class="panel-heading">Panel heading without title</div>
                <div class="panel-body">
                    筛选
                </div>
            </div>
        </div>
        <div class="col-sm-9">
            <div class="panel panel-default">
                <!-- Default panel contents -->
                <div class="panel-heading">Panel heading</div>
                <div class="panel-body">
                    <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#addModal">新建问题</a>
                    <a class="btn btn-primary btn-sm" data-toggle="modal" data-target="#inviteModal">邀请成员</a>
                </div>

                <!-- Table -->
                <table class="table">
                    <tbody class="issues-list">
                    {% for item in issues_object_list %}
                        <tr>
                            <td class="number">
                                <i class="fa fa-circle text-{{ item.priority }}"></i>
                                <a target="_blank"
                                   href="{% url 'issues_detail' project_id=request.bug_platform.project.id issues_id=item.id %}">{% string_just item.id %}</a>
                            </td>
                            <td class="issue">
                                <div>
                                    <a target="_blank"
                                       href="{% url 'issues_detail' project_id=request.bug_platform.project.id issues_id=item.id %}">{{ item.subject }}</a>
                                </div>
                                <div class="tags">
                                    <span class="type" style="background-color:#56b8ea;">{{ item.issues_type }}</span>
                                    <span>
                                        <i class="fa fa-refresh" aria-hidden="true"> {{ item.get_status_display }}</i>
                                    </span>
                                    <span>
                                        <i class="fa fa-user-o" aria-hidden="true"> {{ item.creator }}</i>
                                    </span>
                                    {% if item.assign %}
                                        <span>
                                        <i class="fa fa-hand-o-right" aria-hidden="true"> {{ item.assign.username }}</i>
                                        </span>
                                    {% endif %}

                                    {% if item.end_date %}
                                        <span>
                                        <i class="fa fa-clock-o" aria-hidden="true"> {{ item.end_date }} 截止</i>
                                        </span>
                                    {% endif %}

                                    <span>
                                        <i class="fa fa-clock-o" aria-hidden="true"> {{ item.latest_update_datetime }} 更新</i>
                                    </span>
                                </div>
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
            {#            分页#}
            <nav aria-label="...">
                <ul class="pagination" style="padding-top: 0;">
                    {{ page_string }}
                </ul>
            </nav>
        </div>
    </div>

    {#    新建问题模态框#}
    <div id="addModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新建问题</h4>
                </div>
                <div class="modal-body" style="padding-right: 40px;">

                    <form id="addForm" class="form-horizontal">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="{{ form.issues_type.id_for_label }}" class="col-sm-2 control-label">{{ form.issues_type.label }}</label>
                            <div class="col-sm-10">
                                <div>
                                    {{ form.issues_type }}
                                </div>
                                <div class="error-msg"></div>
                            </div>
                            <div class="error-msg"></div>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.subject.id_for_label }}" class="col-sm-2 control-label">{{ form.subject.label }}</label>
                            <div class="col-sm-10">
                                <div>
                                    {{ form.subject }}
                                </div>
                                <div class="error-msg"></div>
                            </div>

                        </div>
                        <div class="form-group">
                            <label for="{{ form.module.id_for_label }}" class="col-sm-2 control-label">{{ form.module.label }}</label>
                            <div class="col-sm-10">
                                <div>
                                    {{ form.module }}
                                </div>
                                <div class="error-msg"></div>
                            </div>

                        </div>
                        <div class="form-group">
                            <label for="{{ form.desc.id_for_label }}" class="col-sm-2 control-label">{{ form.desc.label }}</label>
                            <div class="col-sm-10">
                                <div id="editor">
                                    {{ form.desc }}
                                </div>
                                <div class="error-msg"></div>
                            </div>

                        </div>

                        <div class="form-group clearfix">
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.status.id_for_label }}" class="col-sm-3 control-label">{{ form.status.label }}</label>
                                    <div class="col-sm-9">
                                        <div>
                                            {{ form.status }}
                                        </div>
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.priority.id_for_label }}" class="col-sm-3 control-label">{{ form.priority.label }}</label>
                                    <div class="col-sm-9">
                                        <div>
                                            {{ form.priority }}
                                        </div>
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.assign.id_for_label }}" class="col-sm-3 control-label">{{ form.assign.label }}</label>
                                    <div class="col-sm-9">
                                        {{ form.assign }}
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.attention.id_for_label }}" class="col-sm-3 control-label">{{ form.attention.label }}</label>
                                    <div class="col-sm-9">
                                        {{ form.attention }}
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.start_date.id_for_label }}" class="col-sm-3 control-label">{{ form.start_date.label }}</label>
                                    <div class="col-sm-9">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                            {{ form.start_date }}
                                        </div>
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.end_date.id_for_label }}" class="col-sm-3 control-label">{{ form.end_date.label }}</label>
                                    <div class="col-sm-9">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                            {{ form.end_date }}
                                        </div>
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.mode.id_for_label }}" class="col-sm-3 control-label">{{ form.mode.label }}</label>
                                    <div class="col-sm-9">
                                        <div>
                                            {{ form.mode }}
                                        </div>
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                            <div class="col-sm-1"></div>
                            <div class="col-sm-5 pd-0">
                                <div class="form-group">
                                    <label for="{{ form.parent.id_for_label }}" class="col-sm-3 control-label">{{ form.parent.label }}</label>
                                    <div class="col-sm-9">
                                        <div>
                                            {{ form.parent }}
                                        </div>
                                        <div class="error-msg"></div>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                    <button id="btnAddSubmit" type="button" class="btn btn-primary">添 加</button>
                </div>
            </div>
        </div>
    </div>

    {#    邀请成员模态框#}
    <div class="modal fade" id="inviteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content clearfix">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">邀请成员</h4>
                </div>
                <div class="modal-body">
                    <form id="inviteForm">
                        {% csrf_token %}
                        {% for item in invite_form %}
                            <div class="form-group">
                                <label for="{{ item.id_for_label }}">{{ item.label }}</label>
                                (<span>
                                {% if item.help_text %}
                                    {{ item.help_text }}
                                {% endif %}
                            </span>)
                                {{ item }}
                                <span class="error-msg"></span>
                            </div>
                        {% endfor %}
                        <button id="btnGenInviteCode" type="button" class="btn btn-success">生成邀请码</button>
                    </form>
                    <div class="hide" id="inviteArea">
                        <hr>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-btn">
                                    <input type="button" value="邀请链接" class="btn btn-default">
                                </div>
                                <input type="text" value="邀请连接" class="form-control" id="inviteUrl">
                                <div class="input-group-btn">
                                    <input type="button" value="复制链接" class="btn btn-primary" id="btnCopyUrl">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'plugins/editor-md/editormd.amd.js' %}"></script>

    <script src="{% static 'plugins/bootstrap-select/dist/js/bootstrap-select.min.js' %}"></script>
    <script src="{% static 'plugins/bootstrap-select/dist/js/i18n/defaults-zh_CN.min.js' %}"></script>

    <script src="{% static 'plugins/bootstrap-datepicker/js/bootstrap-datepicker.js' %}"></script>
    <script src="{% static 'plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js' %}"></script>
    {#    <script src="{% static 'plugins/bootstrap-datepicker/js/bootstrap-datepicker.js' %}"></script>#}
    {#    <script src="{% static 'plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js' %}"></script>#}


    <script>
        var WIKI_UPLOAD_URL = "{% url 'wiki_upload' project_id=request.bug_platform.project.id %}"
        var POST_ISSUES_URL = "{% url 'issues' project_id=request.bug_platform.project.id  %}"
        var INVITE_URL = "{% url 'invite_url' project_id=request.bug_platform.project.id %}"

        $(function () {
            btnAddSubmit();
            binBootSteapShowEvent();
            initDatePicker();   // 出错了

            btnGenInviteCode();    // 邀请成员按钮
            btnCopyUrl();     // 点击拷贝邀请码
        });

        /*
        初始化bootstrap-datepicker 插件
        * */
        function initDatePicker() {
            {#$.noConflict();#}
            console.log(5)
            $('#id_start_date, #id_end_date').datepicker({
                format: 'yyyy-mm-dd',   // 选后的展示格式
                startDate: '0',      //  从什么时候开始选择，0表示从中间开始选，以前的不能选
                language: 'zh-CN',  //  中文显示
                autoclose: true     // 点击其他地方 选择框关闭
            });
        }

        /*
        函数的执行顺序
        */
        function binBootSteapShowEvent() {
            $('#addModal').on('shown.bs.modal', function (evebt) {
                // 对话框弹出时,内容触发
                initEditonMd();
            });
        }

        /*
        初始化markdown编辑器(textare转化为编辑器)
        * */
        function initEditonMd() {
            editormd('editor', {
                placeholder: '请输入内容',
                width: "98%",
                height: 300,
                path: "{% static 'plugins/editor-md/lib/' %}",
                imageUpload: true,  // 支持本地上传
                imageFormats: ['jpg', 'png', 'jpeg', 'gif'],
                imageUploadURL: WIKI_UPLOAD_URL,
            });
        }

        /*
        ajax 发送数据到后台添加
        * */
        function btnAddSubmit() {
            $('#btnAddSubmit').click(function () {

                // 点击之前清除错误
                // $('#addForm').find('.error-msg').empty();
                $.ajax({
                    uri: POST_ISSUES_URL,
                    type: 'POST',
                    data: $('#addForm').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res)
                        if (res.status) {
                            // 刷新
                            window.location.reload();
                        } else {
                            // 展示错误信息
                            $.each(res.error, function (k, v) {
                                $("#id_" + k).parent().next('.error-msg').text(v[0]);
                                {#$.each(res.error, function (key, value) {#}
                                {#    $('#id_' + key).next().text(value[0])#}
                            });
                        }
                    }
                });
            });
        }

        /*
        点击邀请成员按钮
         */
        function btnGenInviteCode() {
            $('#btnGenInviteCode').click(function () {
                $('.error-msg').empty();
                $.ajax({
                    url: INVITE_URL,
                    type: "POST",
                    data: $('#inviteForm').serialize(),   // 表单所有数据
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res)
                        if (res.status) {
                            // 删除class属性hide 并添加后台返回来的邀请码url
                            $('#inviteArea').removeClass('hide').find('#inviteUrl').val(res.data);
                        } else {
                            $.each(res.error, function (k, v) {
                                $("#id_" + k).next('.error-msg').text(v[0]);
                            });
                        }
                    }
                });
            });
        }

        /*
        复制邀请码
         */
        function btnCopyUrl() {

            $('#btnCopyUrl').click(function () {
                // 找到输入框  加上[0] 把jquery对象 变成 document对象
                var textInput = $('#inviteUrl')[0];         // 相等于 document.getElementById('inviteUrl')
                textInput.select();                         // 选中输入框内容
                document.execCommand('Copy');               // 选中后拷贝
                alert('复制成功');
            })
        }
    </script>
{% endblock %}